package com.contory.ssn_lgnd.framework.gl;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLES20;

public class Vertices {
    final int vertexSize;
    final FloatBuffer vertices;
    final ShortBuffer indices;
    final boolean hasColor;
    final boolean hasTexCoords;
    
    public Vertices(int maxVertices, int maxIndices, boolean hasColor, boolean hasTexCoords) {
    	this.hasColor = hasColor;
        this.hasTexCoords = hasTexCoords;
        this.vertexSize = (2 + (hasColor?4:0) + (hasTexCoords?2:0)) * 4;
        ByteBuffer buffer = ByteBuffer.allocateDirect(maxVertices * vertexSize);
        buffer.order(ByteOrder.nativeOrder());
        vertices = buffer.asFloatBuffer();
        
        if(maxIndices > 0) {
            buffer = ByteBuffer.allocateDirect(maxIndices * Short.SIZE / 8);
            buffer.order(ByteOrder.nativeOrder());
            indices = buffer.asShortBuffer();
        } else {
            indices = null;
        }            
    }
    
    public void setVertices(float[] vertices, int offset, int length) {
        this.vertices.clear();
        this.vertices.put(vertices, offset, length);
        this.vertices.flip();
    }
    
    public void setIndices(short[] indices, int offset, int length) {
        this.indices.clear();
        this.indices.put(indices, offset, length);
        this.indices.flip();
    }
    
    public void bind(ESShader shader, String name_h_pos, String name_h_color, String name_h_texCoor, String name_h_hasColor) {
    	GLES20.glEnableVertexAttribArray(shader.getAttribute(name_h_pos));
    	vertices.position(0);
    	GLES20.glVertexAttribPointer(shader.getAttribute(name_h_pos), 2, GLES20.GL_FLOAT, false, vertexSize, vertices);
    	
    	if(hasColor) {
    		GLES20.glUniform1i(shader.getUniform(name_h_hasColor), 1);
    		GLES20.glEnableVertexAttribArray(shader.getAttribute(name_h_color));
	    	vertices.position(2);
	    	GLES20.glVertexAttribPointer(shader.getAttribute(name_h_color), 4, GLES20.GL_FLOAT, false, vertexSize, vertices);
    	}
    	else
    		GLES20.glUniform1i(shader.getUniform(name_h_hasColor), 0);
		
    	if(hasTexCoords) {
	    	GLES20.glEnableVertexAttribArray(shader.getAttribute(name_h_texCoor));
	        vertices.position(hasColor?6:2);
	    	GLES20.glVertexAttribPointer(shader.getAttribute(name_h_texCoor), 2, GLES20.GL_FLOAT, false, vertexSize, vertices);
    	}
    }

	public void draw(int primitiveType, int offset, int numVertices) {        
	    if(indices!=null) {
	        indices.position(offset);
	        GLES20.glDrawElements (primitiveType, numVertices, GLES20.GL_UNSIGNED_SHORT, indices);
	    } else {
	        GLES20.glDrawArrays(primitiveType, offset, numVertices);
	    }        
	}

	public void unbind(ESShader shader, String name_h_color, String name_h_texCoor) {
		if(hasTexCoords)
			GLES20.glDisableVertexAttribArray(shader.getAttribute(name_h_texCoor));
		if(hasColor)
			GLES20.glDisableVertexAttribArray(shader.getAttribute(name_h_color));
    }
}
